﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EventRegistration;


public partial class CreateProductForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CurrentData.RegistrationInfo.Completed) //If user not login, redirect to login page
        {
            Response.Redirect("../Register/Login.aspx");
        }
    }

    protected void createNewProductButton_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection connection = null;

            //Create query string
            string sql = "insert into Product" +
                  " (Model,Type,Price) values " +
                  " (@Model,@Type,@Price)";

            //Get the connection string
            string url1 = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();


            try
            {
                connection = new SqlConnection(url1); //Create connection
                connection.Open(); //Open connection
                SqlCommand cmd = new SqlCommand(sql, connection); //Create query command form query string

                #region Add parameters to the query command
                SqlParameter para = new SqlParameter("@Model", System.Data.SqlDbType.VarChar);
                para.Value = nameTextbox.Text;
                cmd.Parameters.Add(para);
                para = new SqlParameter("@Type", System.Data.SqlDbType.VarChar);
                para.Value = typeDropDownList.SelectedItem.Text;
                cmd.Parameters.Add(para);
                para = new SqlParameter("@Price", System.Data.SqlDbType.Float);

                para.Value = double.Parse(priceTextbox.Text);


                cmd.Parameters.Add(para);
                #endregion

                cmd.ExecuteNonQuery(); //Execute command
                Labelshow.Text = "New Product is created";

            }
            catch (Exception f)
            {
                Labelshow.Text = f.Message;
            }
            finally
            {
                try
                {

                    connection.Close(); //Close connection
                }
                catch (Exception g)
                {
                    Labelshow.Text = g.Message;
                }
            }
        }
        catch (Exception ex)
        {

            Labelshow.Text = ex.Message;

        }

    }
    protected void viewProductButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProducts.aspx");
    }
}

